//
//  CircleCell.m
//  CircleCellDemo
//
//  Created by nd on 2019/1/9.
//  Copyright © 2019 ND. All rights reserved.
//

#import "CircleCell.h"
#import "XLPhotoBrowser.h"

#define itemWidth (SCREENWIDTH-50)/3.0

@implementation CircleCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {
    
    NSLog(@"+++++:%ld",  gestureRecognizer.view.tag);

    NSMutableArray *pics = [NSMutableArray array];
    for(NSInteger i=0; i<_mode.picList.count; i++){
        [pics addObject:[UIImage imageNamed:_mode.picList[i]]];
    }
    
    UIWindow *window = [[UIApplication sharedApplication].windows objectAtIndex:0];
    CGRect convertRect = [[gestureRecognizer.view superview] convertRect:gestureRecognizer.view.frame toView:window];
    
   [XLPhotoBrowser showPhotoBrowserWithImages:pics currentImageIndex:gestureRecognizer.view.tag-100 withLocation:convertRect];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _headerView = [UIImageView new];
        [self addSubview:_headerView];
        _headerView.backgroundColor = [UIColor grayColor];
        [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self).offset(15);
            make.height.width.mas_equalTo(50);
        }];
        
        _name = [UILabel new];
        [self addSubview:_name];
        [_name mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.headerView.mas_centerY);
            make.left.equalTo(self.headerView.mas_right).offset(10);
            make.right.equalTo(self).offset(-15);
            make.height.equalTo(self.headerView.mas_height);
        }];
        
        _textView = [UILabel new];
        _textView.numberOfLines = 5;
        [self addSubview:_textView];
        [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(15);
            make.right.equalTo(self).offset(-15);
            make.top.equalTo(self.name.mas_bottom).offset(10);
        }];
        
        for (NSInteger i=0; i<9; i++) {
            UIImageView *imageView = [UIImageView new];
            imageView.tag = 100+i;
            imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
            [imageView addGestureRecognizer:singleTap];
            imageView.backgroundColor = [UIColor yellowColor];
            [self addSubview:imageView];
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self);
                make.top.equalTo(self.textView.mas_bottom).offset(10);
                make.width.height.mas_equalTo(0);
                if (i==0) {
                    make.bottom.equalTo(self).offset(-5);
                }
                
            }];
        }
        
    }
    return self;
}
-(void)setMode:(CircleCellMode *)mode{
    _mode = mode;
    
    _name.text = _mode.name;
    _headerView.image = _mode.headImage;
    _textView.text = _mode.content;
     for (NSInteger i=0; i<9; i++) {
         UIImageView *imageView = [self viewWithTag:100+i];
         if ( i<mode.picList.count) {
//             imageView.image = [UIImage imageNamed:mode.picList[i]];
             imageView.contentMode = UIViewContentModeScaleAspectFill;
             imageView.layer.masksToBounds = YES;
             [imageView sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:mode.picList[i]]];
         }
         
     }
    if(mode.picList.count==0){
        for (NSInteger i=0; i<9; i++) {
            UIImageView *imageView = [self viewWithTag:100+i];
            [imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self);
                make.top.equalTo(self.textView.mas_bottom).offset(10);
                make.width.height.mas_equalTo(0);
                if (i==0) {
                    make.bottom.equalTo(self).offset(-5);
                }
            }];
        }
    }
    else if (mode.picList.count==1) {
        
        for (NSInteger i=0; i<9; i++) {
            UIImageView *imageView = [self viewWithTag:100+i];
            if (i==0) {
                [imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self).offset(15);
                    make.right.equalTo(self).offset(-15);
                    make.top.equalTo(self.textView.mas_bottom).offset(10);
                    make.height.mas_equalTo(250);
                    make.bottom.equalTo(self).offset(-10);
                }];
            }
            else{
                [imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self);
                    make.top.equalTo(self.textView.mas_bottom).offset(10);
                    make.width.height.mas_equalTo(0);
                    if (i==0) {
                        make.bottom.equalTo(self).offset(-5);
                    }
                }];
            }
        }
    }
    else{
        for (NSInteger i=0; i<9; i++) {
            UIImageView *imageView = [self viewWithTag:100+i];
            if (i>=mode.picList.count) {
                [imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self);
                    make.top.equalTo(self.textView.mas_bottom).offset(10);
                    make.width.height.mas_equalTo(0);
                }];
            }
            else{
                if (i==3) {
                    imageView.backgroundColor = [UIColor redColor];
                }
                [imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    
                    if(i==mode.picList.count-1){
                        make.bottom.equalTo(self).offset(-10);
                    }
                    make.left.equalTo(self).offset(15+itemWidth*(i%3)+10*(i%3));
                    make.width.mas_equalTo(itemWidth);
                    make.top.equalTo(self.textView.mas_bottom).offset(10+(10*(i/3))+i/3*itemWidth);
                    make.height.mas_equalTo(itemWidth);
                    
                }];
            }
            
        }
    }
    
    
}

@end
